package org.algorithms.ricartagrawala;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class AddressDialog extends JDialog implements ActionListener {
	private final JPanel contentPanel = new JPanel();
	private JTextField ipHostC1;
	private JTextField ipHostC2;
	private JTextField ipHostC3;
	private JTextField ipTrain;
	private boolean valid;
	private ProcessCi[] processes = new ProcessCi[3];
//	private Integer numberOfProcesses;
	private InetAddress ipTrainAddress;
	private JTextField numCi;
	private int pid;

	/**
	 * Create the dialog.
	 */
	public AddressDialog() {
//		setNumberOfProcesses(
//				new Integer(
//						ConfigLoader.configBundle.getString("number.of.processes")));
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setModal(true);
		setTitle("Address Dialog");
		setResizable(false);
		setBounds(100, 100, 236, 196);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new GridLayout(5, 2, 0, 0));
		{
			JLabel lblNumCi = new JLabel("NumCi:  ");
			lblNumCi.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(lblNumCi);
		}
		{
			numCi = new JTextField();
			contentPanel.add(numCi);
			numCi.setColumns(10);
		}
		{
			JLabel lblIpHostC1 = new JLabel("C1 IP/Hostname:  ");
			lblIpHostC1.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(lblIpHostC1);
		}
		{
			ipHostC1 = new JTextField();
			ipHostC1.setText("127.0.0.1");
			contentPanel.add(ipHostC1);
			ipHostC1.setColumns(10);
		}
		{
			JLabel lblIpHostC2 = new JLabel("C2 IP/Hostname:  ");
			lblIpHostC2.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(lblIpHostC2);
		}
		{
			ipHostC2 = new JTextField();
			ipHostC2.setText("127.0.0.1");
			contentPanel.add(ipHostC2);
			ipHostC2.setColumns(10);
		}
		{
			JLabel lblIpHostC3 = new JLabel("C3 IP/Hostname:  ");
			lblIpHostC3.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(lblIpHostC3);
		}
		{
			ipHostC3 = new JTextField();
			ipHostC3.setText("127.0.0.1");
			contentPanel.add(ipHostC3);
			ipHostC3.setColumns(10);
		}
		{
			JLabel lblIpTrain = new JLabel("IP Trem:  ");
			lblIpTrain.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(lblIpTrain);
		}
		{
			ipTrain = new JTextField();
			ipTrain.setText("127.0.0.1");
			contentPanel.add(ipTrain);
			ipTrain.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener( new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						System.exit(0);
					}
				});
				buttonPane.add(cancelButton);
			}
		}
	}

		
	public boolean validateFields(JDialog dialog) throws Exception {
		return true;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		dispose();
	}
	
	public ProcessCi[] buildProcessCi() throws UnknownHostException {
		ProcessCi c1 = new ProcessCi();
		c1.setPid(1);
		InetAddress ipAddressC0 = InetAddress.getByName( getIpHostC1().getText() );
		c1.setIpAddress( ipAddressC0 );
		
		ProcessCi c2 = new ProcessCi();
		c2.setPid(2);
		InetAddress ipAddressC1 = InetAddress.getByName( getIpHostC2().getText() );
		c2.setIpAddress( ipAddressC1 );
		
		ProcessCi c3 = new ProcessCi();
		c3.setPid(3);
		InetAddress ipAddressC2 = InetAddress.getByName( getIpHostC3().getText() );
		c3.setIpAddress( ipAddressC2 );
		
		int i=0;
		processes[i++] = c1;
		processes[i++] = c2;
		processes[i++] = c3;
		
		setPid( new Integer(numCi.getText()).intValue() );
		
		InetAddress ipTrain = InetAddress.getByName( getIpTrain().getText() );
		processes[getPid() - 1].setIpTrain(ipTrain);
		
		return processes;
	}

	@Override
	public boolean isValid() {
		return valid;
	}
	
	public void setValid(boolean valid) {
		this.valid = valid;
	}
	
	/*
	 * SETTERS AND GETTERS
	 */
	private JTextField getIpHostC1() {
		return ipHostC1;
	}

	private JTextField getIpHostC2() {
		return ipHostC2;
	}

	private JTextField getIpHostC3() {
		return ipHostC3;
	}

	private JTextField getIpTrain() {
		return ipTrain;
	}

	public ProcessCi[] getProcesses() {
		return processes;
	}

	public InetAddress getIpTrainAddress() {
		return ipTrainAddress;
	}

	public void setIpTrainAddress(InetAddress ipTrainAddress) {
		this.ipTrainAddress = ipTrainAddress;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}
}